package com.vueblog.webapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.vueblog.webapi.entity.BlogEntity;
import com.vueblog.webapi.entity.UserEntity;
import com.vueblog.webapi.enums.ResponseEnum;
import com.vueblog.webapi.exception.BusinessException;
import com.vueblog.webapi.mapper.UserMapper;
import com.vueblog.webapi.request.UpdateUserInfoRequest;
import com.vueblog.webapi.response.BaseResponse;
import com.vueblog.webapi.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vueblog.webapi.utils.MD5Utils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luosj
 * @since 2021-05-16
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserEntity checkUser(String username, String password) {
        UserEntity user = userMapper.selectOne(new QueryWrapper<UserEntity>().lambda()
                .eq(UserEntity::getUsername, username)
                .eq(UserEntity::getPassword, MD5Utils.code(password)));
        return user;
    }

    /**
     * 根据用户名查询用户信息
     * @param username
     * @return
     */
    @Override
    public UserEntity getUserByName(String username) {
        UserEntity user = userMapper.selectOne(new QueryWrapper<UserEntity>().lambda()
                .eq(UserEntity::getUsername, username));
        return user;
    }

    /**
     * 获取当前用户信息
     */
    @Override
    public BaseResponse getCurrentUser() {
        // 获取当前的用户
        Subject subject = SecurityUtils.getSubject();
        UserEntity user =(UserEntity) subject.getPrincipal();
        if (user != null) {
            return new BaseResponse(ResponseEnum.SUCCESS, user);
        } else {
            return new BaseResponse(ResponseEnum.ERROR, "系统无登录用户");
        }
    }

    /**
     * 修改当前用户信息
     */
    @Transactional
    @Override
    public BaseResponse updateUserInfo(UpdateUserInfoRequest request, String id) {
        // 根据用户id查询用户
        UserEntity user = userMapper.selectOne(new QueryWrapper<UserEntity>().lambda()
                .eq(UserEntity::getId, id));
        if (user == null) {
            throw new BusinessException("当前用户不存在");
        }
        // 获取当前用户的密码
        String password = user.getPassword();
        // 加密请求过来的oldPassword
        String oldPassword = MD5Utils.code(request.getOldPassword());
        // 判断原密码是否正确
        if (!oldPassword.equals(password)) {
            throw new BusinessException("原密码不正确，请重新输入");
        }
        // 设置user对象需要修改的信息
        user
                .setUsername(request.getNewUserName())
                .setPassword(MD5Utils.code(request.getNewPassword()))
                .setUpdateTime(LocalDateTime.now());
        // 修改用户信息
        userMapper.updateById(user);
        // 返回成功信息信息
        return new BaseResponse(ResponseEnum.SUCCESS);
    }

    /**
     * 设置用户登录状态
     * @param isOnLine
     */
    @Override
    public void setIsOnLine(boolean isOnLine) {
        UpdateWrapper<UserEntity> userEntityUpdateWrapper = new UpdateWrapper<>();
        userEntityUpdateWrapper
                .set("is_on_line", isOnLine);
        userMapper.update(null, userEntityUpdateWrapper);
    }

    /**
     * 判断用户是否在线
     * @return
     */
    @Override
    public BaseResponse getIsOnLine() {
        Integer onLineCount = userMapper.selectCount(new QueryWrapper<UserEntity>().lambda()
                .eq(UserEntity::getIsOnLine, true));
        if (onLineCount == 1) {
            return new BaseResponse(ResponseEnum.SUCCESS);
        }
        return new BaseResponse(ResponseEnum.ERROR);
    }

}
